<template>
  <div class="section-dialog">
    <el-dialog
      :title="isEdit ? '更新课时' : '添加课时'"
      :visible="show"
      @close="$emit('changeDialog', false)"
      width="40%"
    >
      <el-form
        class="custom-form"
        ref="form"
        :model="lesson"
        label-width="80px"
      >
        <el-form-item class="custom-form-item" label="课程名称">
          <el-input v-model="courseName" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item class="custom-form-item" label="章节名称">
          <el-input v-model="sectionName" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item class="custom-form-item" label="课时名称">
          <el-input v-model="lesson.theme"></el-input>
        </el-form-item>
        <el-form-item class="custom-form-item" label="时长">
          <el-input v-model="lesson.duration" type="number">
            <template slot="append">分钟</template>
          </el-input>
        </el-form-item>
        <el-form-item class="custom-form-item" label="是否开放试听">
          <el-switch
            v-model="lesson.isFree"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </el-form-item>
        <el-form-item class="custom-form-item" label="课时排序">
          <el-input v-model="lesson.orderNum" type="number">
            <template slot="append">数字控制排序，数字越大越靠后</template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="$emit('changeDialog', false)">取消</el-button>
        <el-button type="primary" @click="changeLesson">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import Vue from 'vue'
import {
  getLessonById,
  createOrUpdateLesson,
  CreateOrUpdateLessonType
} from '@/apis/lesson'
export default Vue.extend({
  name: 'create-or-update-lesson',
  props: {
    show: {
      type: Boolean,
      default: false
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    sectionId: {
      type: Number
    },
    lessonId: {
      type: Number
    },
    courseId: {
      type: [String, Number]
    },
    courseName: {
      type: String,
      default: ''
    },
    sectionName: {
      type: String,
      default: ''
    }

  },
  data() {
    return {
      lesson: {} as CreateOrUpdateLessonType
    }
  },
  created() {
    this.isEdit && this.loadLesson()
  },
  methods: {
    async loadLesson() {
      const { data } = await getLessonById(this.lessonId)
      if (data.code === '000000') {
        const current = data.data
        this.lesson = {
          id: current.id,
          courseId: current.courseId,
          sectionId: current.sectionId,
          theme: current.theme,
          duration: current.duration || 0,
          isFree: current.isFree,
          status: current.status,
          orderNum: current.orderNum
        }
      }
    },
    async changeLesson() {
      const { data } = await createOrUpdateLesson({
        ...this.lesson,
        courseId: this.courseId,
        sectionId: this.sectionId
      })
      if (data.code === '000000') {
        this.$message.success(this.isEdit ? '更新课时成功' : '添加课时成功')
        this.$emit('changeDialog', false, true)
      }
    }
  }
})
</script>
<style lang="scss" scoped>
.section-dialog {
  .custom-form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .custom-form-item {
      width: 70%;
    }
  }
}
</style>
